package storage

import (
	"github.com/jmoiron/sqlx"
	"github.com/pkg/errors"
	. "web_framework/model"
)

func RegisterCreateUser(db sqlx.Execer, qi *RegisterUser) (int, error) {

	re := sqlx.MustExec(db, `INSERT INTO user (salt, nickname, password, phone_area, phone_number, pinyin, point, userpic, type_id, create_ip, status, common_group_id) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, 52)`,
		qi.Salt,qi.Nickname,qi.Password,qi.PhoneArea,qi.PhoneNumber,qi.PinYin,0,"avatar/f_noavatar_b.gif",0,qi.CreateIp)

	uid, err := re.LastInsertId()
	if err != nil {
		return 0, err
	}

	return int(uid), nil
}
func RegisterInitUserExtend(db sqlx.Execer, uid int) (int, error) {
	re := sqlx.MustExec(db, `INSERT INTO user_extend (uid) VALUES (?)`, uid)
	reply, err := re.LastInsertId()
	if err != nil {
		return 0, err
	}
	return int(reply), nil
}
func RegisterInitCount(db sqlx.Execer, uid int) (int, error) {
	re := sqlx.MustExec(db, `INSERT INTO user_count (uid) VALUES (?)`, uid)
	reply, err := re.LastInsertId()
	if err != nil {
		return 0, err
	}
	return int(reply), nil
}
func RegisterInitUserAmount(db sqlx.Execer, uid int) (int, error) {
	re := sqlx.MustExec(db, `INSERT INTO user_amount (uid,create_time,update_time) VALUES (?,UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW()))`, uid)
	reply, err := re.LastInsertId()
	if err != nil {
		return 0, err
	}
	return int(reply), nil
}
func RegisterInitUserStatus(db sqlx.Execer, uid int, ip int64 ) (int, error) {
	re := sqlx.MustExec(db, `INSERT INTO user_status (uid,lastvisit,lastactivity,create_time,regip,lastip,create_ip,lastpost,lastreadfav,lastsendmsg,lastreadsysmessage,invisible,status)
VALUES (?,UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW()),?,?,?)`, uid,ip,ip,ip,0,0,0,0,0)
	reply, err := re.LastInsertId()
	if err != nil {
		return 0, err
	}
	return int(reply), nil
}





func GetUserByName(db sqlx.Queryer, name string) (User, error) {
	var qi User
	err := sqlx.Get(db, &qi, `select uid,salt,password,phone_area,phone_number,email,nickname,userpic from user where nickname = ? limit 1`, name)
	if err != nil {
		return qi, err
	}

	return qi, nil
}

func GetUserByEmail(db sqlx.Queryer, email string) (User, error) {
	var qi User
	err := sqlx.Get(db, &qi, `select uid,salt,password,phone_area,phone_number,email,nickname,userpic from user where email = ? limit 1`, email)
	if err != nil {
		return qi, err
	}

	return qi, nil
}

func GetUserByPhone(db sqlx.Queryer, phoneArea int, phoneNumber string) (User, error) {
	var qi User
	err := sqlx.Get(db, &qi, `select uid,salt,password,phone_area,phone_number,email,nickname,userpic from user where phone_number = ? and phone_area = ? limit 1`,
		phoneNumber, phoneArea)
	if err != nil {
		return qi, err
	}
	return qi, nil
}

func GetUserByUid(db sqlx.Queryer, uid int) (User, error) {
	var qi User
	err := sqlx.Get(db, &qi, `select uid, salt, password, phone_area, phone_number, email, nickname from user where uid = ? limit 1`, uid)
	if err != nil {
		return qi, err
	}
	return qi, nil
}


func DeleteUser(db sqlx.Execer, id int) error {
	res, err := db.Exec("delete from user where uid = ?", id)
	if err != nil {
		return err
	}
	ra, err := res.RowsAffected()
	if err != nil {
		return errors.Wrap(err, "get rows affected error")
	}
	if ra == 0 {
		return errors.New("not exist")
	}

	return nil
}

func UpdateUserPassword(db sqlx.Execer, qi *UpdatarUser) error {

	res, err := db.Exec("update user set password = ? where uid = ?", qi.Password, qi.Uid)
	if err != nil {
		return err
	}
	ra, err := res.RowsAffected()
	if err != nil {
		return errors.Wrap(err, "get rows affected error")
	}
	if ra == 0 {
		return errors.New("not exist")
	}
	return nil
}

func InsertUserAmount(db sqlx.Execer, uid int) error {

	res, err := db.Exec("update user set password = ? where uid = ?", uid)
	if err != nil {
		return err
	}
	ra, err := res.RowsAffected()
	if err != nil {
		return errors.Wrap(err, "get rows affected error")
	}
	if ra == 0 {
		return errors.New("not exist")
	}
	return nil
}

func GetUserInfoRelation(db sqlx.Queryer, uid int) (UserInfo, error) {
	var qi UserInfo
	err := sqlx.Get(db, &qi, `select user.uid, nickname, if(main_group_id > 0, main_group_id, common_group_id) as group_id, userpic, point, total_size from user left join user_upload_total on user.uid = user_upload_total.uid where user.uid = ?`, uid)
	if err != nil {
		return qi, err
	}
	return qi, nil
}

func GetGroupInfo(db sqlx.Queryer, groupId int) (GroupInfo, error) {
	var qi GroupInfo
	err := sqlx.Get(db, &qi, `select group_id, group_name from group_info where group_id = ?`, groupId)
	if err != nil {
		return qi, err
	}
	return qi, nil
}
